package top.xmln.githublogin_demo.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.xmln.githublogin_demo.service.RequestService;

import java.io.IOException;

@RestController
public class RequestController {
    @Resource
    private RequestService service;

    /**
     * 回调地址
     * @param code 授权码
     */
    @GetMapping("/redirect")
    public Object redirect(@RequestParam("code") String code) {
        return service.sendAuthorizationCode(code);
    }

    /**
     * 拼接授权链接地址返回重定向
     * @param response 响应对象
     */
    @GetMapping("/")
    public void index(HttpServletResponse response) {
        String authorizationLink = service.getAuthorizationLink();

        try {
            response.sendRedirect(authorizationLink);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
